/*
 *  Copyright (C) 2011 Hatsites
 * 
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package org.myorg.shr.gui.swg.util.property;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 *
 * @author Hatsites
 *
 * email: Hatsites_AT_gmail.com
 * ... please remove "_AT_" from the above string to get the right email address
 */

abstract public class PrpMgrAbs {

    protected PrpMgrAbs(String strNameFile){
        PrpMgrAbs._STR_NAME_FILE_ = strNameFile;
    }


    protected static void write(Properties prp, String strPathFile) throws Exception{
        FileOutputStream out = new FileOutputStream(strPathFile);
        prp.storeToXML(out, "");
        out.close();

        //System.out.println("Writing property, writing in file : "  + strPathFile );

        return;
    }


    protected static void read(Properties prp, String strPathFile) throws Exception{


        FileInputStream in = new FileInputStream(strPathFile);
        prp.loadFromXML(in);


        //System.out.println("Reading property, reading in file : "  + strPathFile );

        
        in.close();

        return;
    }


    static public File s_getOrCreateVersionDir() throws
    Exception
	{
		File fleParent = new File(System.getProperty("user.home"));

		if (fleParent == null){
			return null;// error
		}
		if (! fleParent.isDirectory()){
			return null;// error
		}
		if (!fleParent.canRead() || !fleParent.canWrite()){
			fleParent = new File("."); // use install dir instead!
		}

		File fleCur = null;

		for (int i=0; i<PrpNamingAbs.STRS_LOGS_PARENT_FOLDERS.length-1; i++)
		{
			fleCur = new File(fleParent, PrpNamingAbs.STRS_LOGS_PARENT_FOLDERS[i]);

			if (fleCur == null)	{
				return null;// error
			}

            if (! fleCur.exists()){
				fleCur.mkdir();
			}
			else if (! fleCur.isDirectory()){
				return null;//error
			}

			fleParent = fleCur;
		}

		fleCur = new File(fleParent, PrpNamingAbs.STR_APPLI_VERSION_TRANSFORMED);

		if (! fleCur.exists()){
			fleCur.mkdir();
		}
		else if (! fleCur.isDirectory()){
			return null;//error
		}
		return fleCur;
	}

    static private String _STR_NAME_FILE_;
    final static public String _STR_FILEEXT_ = ".props";

}
